﻿using Common.Enums;
using Common.Untils;
using HandyControl.Controls;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Tryit.Views;
using MessageBox = HandyControl.Controls.MessageBox;
namespace Tryit.ViewModels
{
    public class HomeWindowViewModel : BindableBase
    {
        /// <summary>
        /// 页面名称
        /// </summary>
        private string _page;
        public string Page
        {
            get { return _page; }
            set { SetProperty(ref _page, value); }
        }

        /// <summary>
        /// 标题名称
        /// </summary>
        private string _title = "SanmaoTools";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }
        /// <summary>
        /// 收缩或展开按钮名称
        /// &#xe665;为收缩
        /// &#xe668;为展开
        /// </summary>
        private string _expandText = "\xe665";
        public string ExpandText
        {
            get => _expandText;
            set => SetProperty(ref _expandText, value);
        }
        /// <summary>
        /// 是否已经收缩
        /// </summary>
        private bool _isCollapsed;
        public bool IsCollapsed
        {
            get => _isCollapsed;
            set
            {
                if (SetProperty(ref _isCollapsed, value))
                {
                    ExpandText = value ? "\xe668" : "\xe665";
                    SidebarWidth = value ? "48" : "200";
                    RaisePropertyChanged(nameof(SidebarWidth));
                }
            }
        }
        /// <summary>
        /// 侧边栏宽度,默认200
        /// </summary>
        private string _sidebarWidth = "200";
        public string SidebarWidth
        {
            get => _sidebarWidth;
            set => SetProperty(ref _sidebarWidth, value);
        }
        /// <summary>
        /// 切换导航页面
        /// </summary>
        private DelegateCommand<string> _navigateCommand;
        public DelegateCommand<string> NavigateCommand =>
            _navigateCommand ?? (_navigateCommand = new DelegateCommand<string>(ExecuteNavigateCommand));
        /// <summary>
        /// RegionManager导航管理
        /// </summary>
        private readonly IRegionManager _regionManager;

        private string _version;
        public string Version
        {
            get => _version;
            set => SetProperty(ref _version, value);
        }

        private DelegateCommand _toggleSidebarCommand;
        public DelegateCommand ToggleSidebarCommand =>
            _toggleSidebarCommand ?? (_toggleSidebarCommand = new DelegateCommand(ExecuteToggleSidebar));


        private void ExecuteToggleSidebar()
        {
            IsCollapsed = !IsCollapsed;
        }

        public HomeWindowViewModel(IRegionManager regionManager)
        {
            InitPara();
            _regionManager = regionManager;

            // 确保在UI线程上执行导航
            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                // 先设置当前页面
                Page = "TPAutoUpload";
                // 然后执行导航
                _regionManager.RegisterViewWithRegion("ContentRegion", typeof(TPAutoUpload));//一开始是未登录
                Debug.WriteLine("初始化导航到 TPAutoUpload");
            }));

            // 从本地加载版本信息
            var versionInfo = VersionInfo.Load();
            Version = $"Version:{versionInfo.Version}";
        }

        private void ExecuteNavigateCommand(string navigatePath)
        {
            if (string.IsNullOrWhiteSpace(navigatePath))
                return;

            try
            {
                if (Page != navigatePath)
                {
                    Debug.WriteLine($"当前页面: {Page}, 目标页面: {navigatePath}");
                    Page = navigatePath;
                    _regionManager.RequestNavigate("ContentRegion", navigatePath);
                    Debug.WriteLine($"正在导航到: {navigatePath}");
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"导航错误: {ex.Message}");
                Debug.WriteLine($"错误堆栈: {ex.StackTrace}");
                MessageBox.Show($"导航错误: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }


        private void InitPara()
        {
            ExpandText = "\xe665";
            SidebarWidth = "200";
            IsCollapsed = false;
            // 设置初始页面
            Page = "TPAutoUpload";
        }
    }
}
